package org.coody.framework.esource.wrapper;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

import org.coody.framework.esource.ESource;

public class ConnectionWrapper implements Connection {

	private List<StatementWrapper> statements = new ArrayList<StatementWrapper>();

	private Connection source;

	private ESource pool;

	private boolean hasUpdate;

	private Long activeTime = System.currentTimeMillis();

	public void clear() throws SQLException {
		for (StatementWrapper statement : statements) {
			if (statement == null) {
				continue;
			}
			statement.close();
		}
		statements.clear();
	}

	public void clearAndClose() throws SQLException {
		clear();
		this.source.close();
	}

	public Long getActiveTime() {
		return activeTime;
	}

	public void setActiveTime(Long activeTime) {
		this.activeTime = activeTime;
	}

	public ConnectionWrapper(Connection source, ESource pool) {
		super();
		this.source = source;
		this.pool = pool;
	}

	public boolean isHasUpdate() {
		return hasUpdate;
	}

	public void setHasUpdate(boolean hasUpdate) {
		this.hasUpdate = hasUpdate;
	}

	public Connection getSource() {
		return source;
	}

	public void setSource(Connection source) {
		this.source = source;
	}

	public ESource getPool() {
		return pool;
	}

	public void setPool(ESource pool) {
		this.pool = pool;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return source.unwrap(iface);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return source.isWrapperFor(iface);
	}

	@Override
	public Statement createStatement() throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.createStatement());
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareStatement(sql));
		statements.add(statement);
		return statement;
	}

	@Override
	public CallableStatement prepareCall(String sql) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareCall(sql));
		statements.add(statement);
		return statement;
	}

	@Override
	public String nativeSQL(String sql) throws SQLException {
		return source.nativeSQL(sql);
	}

	@Override
	public void setAutoCommit(boolean autoCommit) throws SQLException {
		source.setAutoCommit(autoCommit);
	}

	@Override
	public boolean getAutoCommit() throws SQLException {
		return source.getAutoCommit();
	}

	@Override
	public void commit() throws SQLException {
		source.commit();
	}

	@Override
	public void rollback() throws SQLException {
		source.rollback();
	}

	@Override
	public void close() throws SQLException {
		clear();
		pool.close(this);
	}

	@Override
	public boolean isClosed() throws SQLException {
		return source.isClosed();
	}

	@Override
	public DatabaseMetaData getMetaData() throws SQLException {
		return source.getMetaData();
	}

	@Override
	public void setReadOnly(boolean readOnly) throws SQLException {
		source.setReadOnly(readOnly);
	}

	@Override
	public boolean isReadOnly() throws SQLException {
		return source.isReadOnly();
	}

	@Override
	public void setCatalog(String catalog) throws SQLException {
		source.setCatalog(catalog);
	}

	@Override
	public String getCatalog() throws SQLException {
		return source.getCatalog();
	}

	@Override
	public void setTransactionIsolation(int level) throws SQLException {
		source.setTransactionIsolation(level);
	}

	@Override
	public int getTransactionIsolation() throws SQLException {
		return source.getTransactionIsolation();
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		return source.getWarnings();
	}

	@Override
	public void clearWarnings() throws SQLException {
		source.clearWarnings();
	}

	@Override
	public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.createStatement(resultSetType, resultSetConcurrency));
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
			throws SQLException {
		StatementWrapper statement = new StatementWrapper(
				source.prepareStatement(sql, resultSetType, resultSetConcurrency));
		statements.add(statement);
		return statement;
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareCall(sql, resultSetType, resultSetConcurrency));
		statements.add(statement);
		return statement;
	}

	@Override
	public Map<String, Class<?>> getTypeMap() throws SQLException {
		return source.getTypeMap();
	}

	@Override
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		source.setTypeMap(map);
	}

	@Override
	public void setHoldability(int holdability) throws SQLException {
		source.setHoldability(holdability);
	}

	@Override
	public int getHoldability() throws SQLException {
		return source.getHoldability();
	}

	@Override
	public Savepoint setSavepoint() throws SQLException {
		return source.setSavepoint();
	}

	@Override
	public Savepoint setSavepoint(String name) throws SQLException {
		return source.setSavepoint(name);
	}

	@Override
	public void rollback(Savepoint savepoint) throws SQLException {
		source.rollback(savepoint);
	}

	@Override
	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		source.releaseSavepoint(savepoint);
	}

	@Override
	public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		StatementWrapper statement = new StatementWrapper(
				source.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability));
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
			int resultSetHoldability) throws SQLException {
		StatementWrapper statement = new StatementWrapper(
				source.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
		statements.add(statement);
		return statement;
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
			int resultSetHoldability) throws SQLException {
		StatementWrapper statement = new StatementWrapper(
				source.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareStatement(sql, autoGeneratedKeys));
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareStatement(sql, columnIndexes));
		statements.add(statement);
		return statement;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
		StatementWrapper statement = new StatementWrapper(source.prepareStatement(sql, columnNames));
		statements.add(statement);
		return statement;
	}

	@Override
	public Clob createClob() throws SQLException {
		return source.createClob();
	}

	@Override
	public Blob createBlob() throws SQLException {
		return source.createBlob();
	}

	@Override
	public NClob createNClob() throws SQLException {
		return source.createNClob();
	}

	@Override
	public SQLXML createSQLXML() throws SQLException {
		return source.createSQLXML();
	}

	@Override
	public boolean isValid(int timeout) throws SQLException {
		return source.isValid(timeout);
	}

	@Override
	public void setClientInfo(String name, String value) throws SQLClientInfoException {
		source.setClientInfo(name, value);
	}

	@Override
	public void setClientInfo(Properties properties) throws SQLClientInfoException {
		source.setClientInfo(properties);
	}

	@Override
	public String getClientInfo(String name) throws SQLException {
		return source.getClientInfo(name);
	}

	@Override
	public Properties getClientInfo() throws SQLException {
		return source.getClientInfo();
	}

	@Override
	public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
		return source.createArrayOf(typeName, elements);
	}

	@Override
	public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
		return source.createStruct(typeName, attributes);
	}

	@Override
	public void setSchema(String schema) throws SQLException {
		source.setSchema(schema);
	}

	@Override
	public String getSchema() throws SQLException {
		return source.getSchema();
	}

	@Override
	public void abort(Executor executor) throws SQLException {
		source.abort(executor);
	}

	@Override
	public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
		source.setNetworkTimeout(executor, milliseconds);
	}

	@Override
	public int getNetworkTimeout() throws SQLException {
		return source.getNetworkTimeout();
	}

}
